<?php
require_once "handler.class.php";

/**
 * Handler-class for IF template tag. To be rewritten?
 */
class qIfHandler extends qHandler
{
    /**
     * Constructor.
     */
    function  __construct()
    {
    	$this->iscloseable = true;
    	$this->parsechildren = false;
    }
        
    /**
     * Parses given tag.
     * @see qHandler
     */
    public function Parse($tag, $body)
    {
        // let's get tag's parameter
        $parameter = mb_substr($tag, 4, -1);
        // and change all of variables' occurences to their value
    	$preg = '/\$([a-zA-Z0-9\.]+)/';
		
        $values = qTemplateParser::Values();
		
        preg_match_all($preg, $parameter, $array, PREG_SET_ORDER);
		
        foreach ($array as $elem)
        {
            if (isset($values[$elem[0]]))
            {
                $parameter = str_replace($elem[0], $values[$elem[0]], $parameter);
            }
            else
            {
                // oops, using variable that is not valid...
                return array(false, "qIfHandler Error: variable {$elem[0]} is not set!<br />{$tag}{$body}{endif}");
            }
        }
		
        // check the condition
        eval('$result = '.$parameter.';');
	
        // and return result
        if ($result)
        {
            return array(true, $body);
        }
        else
        {
            return array(true, "");
        }
    }
}